<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "quiz.Account" %>
<%@ page import = "quiz.Quiz" %>
<%@ page import = "quiz.Message" %>
<jsp:include page="templates/header.jsp" flush="true">
	<jsp:param value = "New Message | Quizzard" name = "title" />
</jsp:include>
<jsp:include page="templates/title_bar.jsp" flush = "true">
	<jsp:param value = "New Message" name = "title" />
</jsp:include>
<%
	
	Account a = (Account)session.getAttribute("account");

	Quiz challengedQuiz = null;	
	Account friendingAccount = null;
		
	int type = -1;

	if (request.getAttribute("challengedQuiz") != null) {
		challengedQuiz = (Quiz)request.getAttribute("challengedQuiz");
		type = Message.CHALLENGE;
	} else if (request.getAttribute("friendRequestAccount") != null) {
		friendingAccount = (Account)request.getAttribute("friendRequestAccount");
		type = Message.REQUEST;
	} else if (!a.isAdmin()) {
		type = Message.CHAT;
	}
	
%>
<div id = "content">
<div class = "center">
<%
	String error = (String)request.getAttribute("error");
	if (error != null) {
		out.println("<div id = 'error'>"+error+"</div>");
	}
%>
<form method = "post" action = "SendMessage">
<div class = "form_label"><label for = "messageType">Type: </label></div><div class = "form_field">
<% if (type == -1) { %>
<select name = "messageType"><option value = "<%= Message.CHAT %>">Chat</option><option value = "<%= Message.ANNOUNCEMENT %>">Announcement</option></select>
<%
} else {
	switch(type) {
	case Message.CHALLENGE:
		out.println("Challenge");
		break;
	case Message.REQUEST:
		out.println("Friend Request");
		break;
	case Message.CHAT:
		out.println("Chat");
		break;
	}
}
%>
<input type = "hidden" name = "messageType" value = "<%= type %>" />
</div>
<div class = "clear"></div>
<div class = "form_label"><label for = "to">To: (username)</label></div><div class = "form_field">
<% if (friendingAccount == null) { %>
<input type = "text" name = "usernameOfRecipient" />
<% } else { 
	out.println(friendingAccount.getName()+"<input type = \"hidden\" name = \"usernameOfRecipient\" value = \""+friendingAccount.getUsername()+"\" />");
} %>
</div>
<div class = "form_label"><label for = "from">From: </label></div><div class = "form_field"><span id = "from"><%= a.getName() %></span></div>
<div class = "clear"></div>
<div class = "form_label"><label for = "content">Body: </label></div><div class = "form_field"><textarea name = "messageContent"></textarea></div>
<div class = "clear"></div>
<%
	if (challengedQuiz != null) {
%>
<div class = "form_label"><label for = "">Quiz: </label></div><div class = "form_field"><a href = "ShowSelectedQuiz?id=<%= challengedQuiz.getID() %>"><%= challengedQuiz.getName() %></a></div>
<div class = "clear"></div>
<input type = "hidden" name = "challengedQuiz" value = "<%= challengedQuiz.getID() %>" />
<%
	}
%>
<input type = "submit" class = "btn" value = "Send" />
</form>
</div>
</div>
<jsp:include page="templates/footer.jsp" flush="true" />